package top.zsyle.movie.web.controller;

import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.zsyle.movie.dto.MovieTicketDto;
import top.zsyle.movie.feign.MovieService;
import top.zsyle.movie.result.JsonResult;
import top.zsyle.movie.result.PageList;
import top.zsyle.movie.service.IUserService;
import top.zsyle.user.domain.User;
import top.zsyle.user.query.UserQuery;

import javax.validation.Valid;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    public IUserService userService;

    @Autowired
    public MovieService movieService;

    /**
    * 保存和修改公用的
    */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public JsonResult saveOrUpdate(@RequestBody User user){
        if(user.getId()!=null){
            userService.updateById(user);
        }else{
            userService.insert(user);
        }
        return JsonResult.success();
    }

    /**
    * 删除对象
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public JsonResult delete(@PathVariable("id") Long id){
        userService.deleteById(id);
        return JsonResult.success();
    }

    /**
   * 获取对象
   */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public JsonResult get(@PathVariable("id")Long id){
        return JsonResult.success(userService.selectById(id));
    }

    @RequestMapping(value = "/getMovie/{id}",method = RequestMethod.GET)
    public JsonResult getMovie(@PathVariable("id")Long id){
        JsonResult jsonResult = movieService.get(id);
        if (jsonResult.getData() == null && jsonResult.isSuccess()){
            return JsonResult.error("操作失败");
        }else {
            return jsonResult;
        }

    }


    /**
    * 查询所有对象
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public JsonResult list(){
        return JsonResult.success(userService.selectList(null));
    }


    /**
    * 带条件分页查询数据
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public JsonResult page(@RequestBody UserQuery query){
        Page<User> page = new Page<User>(query.getPage(),query.getRows());
        page = userService.selectPage(page);
        return JsonResult.success(new PageList<User>(page.getTotal(),page.getRecords()));
    }

    /**
     * 电影票查询
     * @return
     */
    @RequestMapping(value = "/movielist",method = RequestMethod.GET)
    public JsonResult Movielist(){
        return JsonResult.success(movieService.list());
    }

    /**
     * 根据电影票Id获取电影票信息
     * @param dto
     * @return
     */
    @RequestMapping(value = "/buy",method = RequestMethod.POST)
    public JsonResult buyById(@RequestBody @Valid MovieTicketDto dto){
        return userService.buyById(dto);
    }

    @RequestMapping(value = "/Consume",method = RequestMethod.POST)
    public JsonResult Consume(@RequestBody @Valid MovieTicketDto dto){
        return userService.Consume(dto);
    }

}
